package com.ecoolex.discount.sales.ws;

import static com.ecoolex.discount.sales.core.CoreConstant.YOU_WORLD_GIFTCARD_CHANNEL_GLOBAL_BUY;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Objects;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecoolex.discount.sales.core.external.v2.YWExternalService;
import com.ecoolex.discount.sales.core.external.v2.pojo.GenerateGiftCardRequest;
import com.ecoolex.discount.sales.core.external.v2.pojo.GenerateGiftCardResponse;
import com.ecoolex.discount.sales.core.manager.YWCardInfoManager;
import com.ecoolex.discount.sales.core.manager.YWCtripOrderManager;
import com.ecoolex.discount.sales.core.manager.YWGiftCardManager;
import com.ecoolex.discount.sales.core.manager.YWGiftCardOrderManager;
import com.ecoolex.discount.sales.entity.YWGiftCard;
import com.ecoolex.discount.sales.entity.YWGiftCardOrder;
import com.ecoolex.discount.sales.pojo.GenerateCtripOrderDTO;
import com.ecoolex.discount.sales.pojo.YWCardInfoDTO;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class YWGiftCardOrderServiceImpl implements YWGiftCardOrderService {

	@Autowired
	YWExternalService ywExternalService;

	@Autowired
	YWCtripOrderManager ywCtripOrderManager;

	@Autowired
	YWGiftCardOrderManager ywGiftCardOrderManager;

	@Autowired
	YWCardInfoManager ywCardInfoManager;

	@Autowired
	YWGiftCardManager ywGiftCardManager;

	final static BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);

	final static MathContext mathContext = new MathContext(2, RoundingMode.HALF_UP);

	@Override
	public YWCardInfoDTO generateGiftCard(GenerateCtripOrderDTO generateCtripOrderDTO) {
		if (YOU_WORLD_GIFTCARD_CHANNEL_GLOBAL_BUY.equalsIgnoreCase(generateCtripOrderDTO.getChannelId())) {
			YWGiftCard ywGiftCard = ywGiftCardManager.getGiftCardByServiceIdAndChannelIdAndCardId(
					generateCtripOrderDTO.getServiceId(), generateCtripOrderDTO.getChannelId(),
					generateCtripOrderDTO.getCardId());
			// 特殊处理礼品卡的面值,避免计算出现精度问题，此字段为必传
			final BigDecimal faceValue = "fixed".equalsIgnoreCase(ywGiftCard.getCardValueType())
					? ywGiftCard.getFaceValue().divide(ONE_HUNDRED)
					: generateCtripOrderDTO.getCounterPrice();
			final BigDecimal discountPercent = ywGiftCard.getCardDiscountPercent();
			final BigDecimal discountPrice = faceValue.multiply(discountPercent.divide(ONE_HUNDRED), mathContext);
			final BigDecimal realPrice = faceValue.subtract(discountPrice);
			generateCtripOrderDTO.setRealPrice(realPrice);
			generateCtripOrderDTO.setDiscountPrice(discountPrice);
			generateCtripOrderDTO.setCardDiscountPercent(discountPercent);
		}
		YWGiftCardOrder ywGiftCardOrder = ywGiftCardOrderManager.save(generateCtripOrderDTO);
		String token = ywExternalService.getToken(generateCtripOrderDTO.getChannelId());
		GenerateGiftCardRequest request = new GenerateGiftCardRequest();
		BeanUtils.copyProperties(generateCtripOrderDTO, request);
		YWCardInfoDTO ywCardInfoDTO = new YWCardInfoDTO();
		if (generateCtripOrderDTO.getStatus() == 1) { // 只有支付成功的才能生成卡号
			GenerateGiftCardResponse response = ywExternalService.generateGiftCard(request, token);
			if (Objects.nonNull(response) && "00".equals(response.getErrno())) {
				GenerateGiftCardResponse.CardInfo cardInfo = response.getData();
				BeanUtils.copyProperties(cardInfo, ywCardInfoDTO);
				ywCardInfoDTO.setResultCode(response.getErrno());
				ywCardInfoDTO.setMessage(response.getErrmsg());
				ywCardInfoDTO.setChannelId(generateCtripOrderDTO.getChannelId());
				ywCardInfoDTO.setServiceId(generateCtripOrderDTO.getServiceId());
				ywCardInfoManager.save(ywCardInfoDTO);
				ywExternalService.pushCardInfoToCtrip(ywCardInfoDTO, "syncMode");
				ywGiftCardOrder.setStatus(1);
			}
			else {
				ywCardInfoDTO.setOrderNo(generateCtripOrderDTO.getOrderNo());
				if (Objects.nonNull(response)) {
					ywCardInfoDTO.setResultCode(response.getErrno());
					ywCardInfoDTO.setMessage(response.getErrmsg());
				}
				else {
					ywCardInfoDTO.setResultCode("02");
					ywCardInfoDTO.setMessage("其他异常");
				}
				ywGiftCardOrder.setStatus(0);
				ywExternalService.schdeule(generateCtripOrderDTO.getServiceId(), generateCtripOrderDTO.getChannelId(),
						generateCtripOrderDTO.getOrderNo());

			}
		}
		else {
			ywGiftCardOrder.setStatus(99);
			ywCardInfoDTO.setResultCode("02");
			ywCardInfoDTO.setMessage("只有支付的订单生成礼品卡");
		}
		ywGiftCardOrderManager.updateStatus(ywGiftCardOrder);
		return ywCardInfoDTO;

	}

}
